﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
   /// <summary>
    /// 文件阅办审批
   /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class TaskCheckController : AdminBaseController
    {
        /// <summary>
        /// 文件阅办审批
        /// </summary>
        /// <returns>返回文件阅办审批主页面视图</returns>
        [AuthorityActionFilter("m_TaskCheck", Order = 1)]
        [LogPageActionFilter("文件阅办审批", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FTaskClassCd in ( select FTaskClassCd from dbo.t_TaskAuthority where FLogID='" + OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) + "'and FAuthorityType='AUTH' ) and  FStatus='2'";
            List<Model.Task> TaskList = GetTaskListList(strwhere, 1);
            BLL.CdTaskClass CdTaskClassBLL = new BLL.CdTaskClass();
            ViewData["CdTaskClassList"] = CdTaskClassBLL.GetModelList("");
            return View(TaskList);
        }

        public ActionResult TaskCheck(string taskID)
        {
            BLL.Task taskBLL = new BLL.Task();
            Model.Task taskModel = new Model.Task();
            if (taskID != null)
            {
                taskModel = taskBLL.GetModel(decimal.Parse(taskID));
            }
            BLL.TaskDo taskDoBLL = new BLL.TaskDo();
            List<Model.TaskDo> taskDoList = taskDoBLL.GetModelList("FTaskID=" + taskID + "");

            ViewData["TaskDoList"] = taskDoList;

            BLL.TaskAttach TaskAttachBLL = new BLL.TaskAttach();
            ViewData["TaskAttachList"] = TaskAttachBLL.GetModelList(" FTaskDoID in (select FTaskDoID from dbo.t_TaskDo where FTaskID=" + taskID + " ) ");
            return View(taskModel);
        }

        public ActionResult Save()
        {
            BLL.Task TaskBLL = new BLL.Task();
            Model.Task TaskModel = TaskBLL.GetModel(decimal.Parse(Fetch.Post("hdFTaskID")));
            try
            {
                bool isADD = true;
                //获取文件类型、大小、名称
                Model.TaskAttach docAttachModel = new Model.TaskAttach();
                string status = Fetch.Post("FStatus");
            
                Model.TaskDo TaskDoModel = new Model.TaskDo();
                TaskDoModel.FInsDate = DateTime.Now;
                TaskDoModel.FInsIP = Common.GenerateUtil.GetClientIPv4Address();
                TaskDoModel.FNote = Fetch.Post("FNote");
                TaskDoModel.FEmplNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                TaskDoModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                TaskDoModel.FTaskID = decimal.Parse(Fetch.Post("hdFTaskID"));
                if (status == "0")
                {
                    TaskDoModel.FStatus = "5";
                    TaskModel.FStatus = "22";
                }
                else if (status == "1")
                {
                    DateTime dt = DateTime.Parse(Fetch.Post("yqdate"));
                    if (TaskModel.FEndDate > DateTime.Parse(dt.ToString("yyyy-MM-dd")))
                    {
                        ViewData["jsAlert"] = "sj";
                        isADD = false;
                    }
                    else
                    {
                        TaskDoModel.FStatus = "6";
                        string txt = TaskDoModel.FNote;
                        TaskDoModel.FNote = "[完成时间从" + TaskModel.FEndDate == null ? "" : DateTime.Parse(TaskModel.FEndDate.ToString()).ToString("yyyy-MM-dd") + "延期至" + dt.ToString("yyyy-MM-dd") + "]  " + txt;
                    }
                    TaskModel.FEndDate = dt;
                    TaskModel.FStatus = "1";
                }
                else if (status == "2")
                {
                    TaskDoModel.FStatus = "7";
                    TaskModel.FStatus = "21";
                }
                else if (status == "3")
                {
                    TaskDoModel.FStatus = "8";
                    TaskModel.FStatus = "5";
                }
                else 
                {
                    TaskDoModel.FStatus = "9";
                    TaskModel.FStatus = "21";
                }

                if (isADD)
                {
                    BLL.Common CommonBLL = new BLL.Common();
                    BLL.TaskDo TaskDoBLL = new BLL.TaskDo();
                    TaskModel.FCheckDate = CommonBLL.GetServerDateTime();
                    TaskModel.FCheckEmpNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                    TaskModel.FCheckIP = Common.GenerateUtil.GetClientIPv4Address();
                    TaskDoBLL.check(TaskDoModel, TaskModel);
                    ViewData["jsAlert"] = "OK";
                }
            }
            catch
            {
                ViewData["jsAlert"] = "error";
            }
            BLL.TaskDo taskDoBLL = new BLL.TaskDo();
            List<Model.TaskDo> taskDoList = taskDoBLL.GetModelList("FTaskID=" + Fetch.Post("hdFTaskID") + "");

            ViewData["TaskDoList"] = taskDoList;
            BLL.TaskAttach TaskAttachBLL = new BLL.TaskAttach();
            ViewData["TaskAttachList"] = TaskAttachBLL.GetModelList(" FTaskDoID in (select FTaskDoID from dbo.t_TaskDo where FTaskID=" + Fetch.Post("hdFTaskID") + " ) ");
            return View("TaskCheck", TaskModel);
        }

        public ActionResult TaskCheckPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
               string lb = Request.Params["lb"];
               string wcqk = Request.Params["wcqk"];

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strWhere = " FTaskClassCd in ( select FTaskClassCd from dbo.t_TaskAuthority where FLogID='" + OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) + "' and FAuthorityType='AUTH') ";
            if (lb != null && lb != "")
            {
                strWhere = " FTaskClassCd = '" + lb + "'";
            }

            if (wcqk != null && wcqk != "")
            {
                if (strWhere != "")
                {
                    strWhere += " and FStatus = '" + wcqk + "'";
                }
                else
                {
                    strWhere += " FStatus = '" + wcqk + "'";
                }
            }
            else
            {
                if (strWhere != "")
                {
                    strWhere += " and FStatus = '" + 2+ "'";
                }
                else
                {
                    strWhere += " FStatus = '" + 2 + "'";
                }
            }
            List<Model.Task> TaskList = GetTaskListList(strWhere, pageIndex);


            return PartialView("TaskCheckPartial", TaskList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.Task> GetTaskListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.Task TaskBLL = new BLL.Task();
            string fiedls = "*";
            int total;
            DataSet ds = commonBll.GetListByPage("dbo.fn_getTask('" + Common.GlobalConstant.PARAM_DEPTNO + "')", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FEndDate", 1, out total);
            List<ElingOA6_Admin_DSJCJ.Model.Task> taskList = TaskBLL.DataTableToList(ds.Tables[0]);
            List<Model.Task> taskAllList = new List<Model.Task>();

            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                taskAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (taskList.Count > i)
                {
                    taskAllList[((pageIndex - 1) * pageRowSize) + i] = taskList[i];
                }
            }

            return taskAllList;
        }

    }
}
